:
eval 'exec perl -wS $0 ${1+"$@"}' if 0;

use File::Temp qw(tmpnam);

my $dir = shift @ARGV;
$dir =~ s/\/$//;
my $tmp_file = tmpnam();

open(TMPFILE, ">$tmp_file");
print TMPFILE $dir;
close TMPFILE;
open (DIRNAME, "/usr/bin/iconv -f windows-1251 -t koi8-r $tmp_file |");
my $new_dir_name = <DIRNAME>;
close DIRNAME;

opendir(DIRECTORY, $dir);
@dir_content = readdir(DIRECTORY);
my @new_dir_content;
closedir(DIRECTORY);
open(TMPFILE, ">$tmp_file");
foreach (@dir_content) {
    chomp;
    next if /^\.+$/;
    push(@new_dir_content, $_);
    print TMPFILE $_ . "\n";
};
close TMPFILE;

open (DIRCONTENT, "/usr/bin/iconv -f windows-1251 -t koi8-r $tmp_file |");
my $i = 0;
foreach (<DIRCONTENT>) {
#     print $_;
     chomp;
     my $old_path = $dir . '/' . $new_dir_content[$i];
     my $new_path = $dir . '/' . $_;
#     print "Rename $dir/". $new_dir_content[$i] . " to $dir/$_\n";
     rename($old_path, $new_path) or system("mv", $old_path, $new_path);
     $i++;     
};
close DIRCONTENT;

rename($dir, $new_dir_name) or system("mv", $dir, $new_dir_name);

unlink $tmp_file;
